#include "gkt_types.h"
#include "gkt_debug.h"
#include "gkt_system.h"
#include "gkt_rtc.h"
#include "gkt_os.h"
#include "gkt_driver.h"
#include "window_common.h"
#include <string.h>

enum {
	TIMER_ID_START = 0,
	TIMER_ID_DO,
	TIMER_ID_TIMEOUT,
	TIMER_ID_EXIT,
	TIMER_ID_MAX
};
enum {
	WIDGET_ID_TITLE,
	WIDGET_ID_TEST_SUCCESS,
	WIDGET_ID_TEST_SUCCESS_CNT,
	WIDGET_ID_TEST_FAILED,
	WIDGET_ID_TEST_FAILED_CNT,
	WIDGET_ID_TEST_GET_FAILED,
	WIDGET_ID_TEST_GET_FAILED_CNT,
	WIDGET_ID_MAX
};

#define WINDOW_AGING_TEST_ITEM_WIDGET_WIDTH (WINDOW_FONT_NORMAL_SIZE*3)
#define WINDOW_AGING_TEST_ITEM_WIDGET_Y WINDOW_WIDGET_NORMAL_HEIGHT
GKT_WINDOW_WIDGET_DESCS_DEFINE(test) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		"test"),
	[WIDGET_ID_TEST_SUCCESS] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_RIGHT,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0,
		WINDOW_AGING_TEST_ITEM_WIDGET_Y,
		WINDOW_FONT_NORMAL_SIZE * 5, WINDOW_WIDGET_NORMAL_HEIGHT,
		"suc:"),
	[WIDGET_ID_TEST_SUCCESS_CNT] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_RIGHT,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_NORMAL_SIZE*5,
		WINDOW_AGING_TEST_ITEM_WIDGET_Y,
		WINDOW_FONT_NORMAL_SIZE * 5, WINDOW_WIDGET_NORMAL_HEIGHT,
		NULL),
	[WIDGET_ID_TEST_FAILED] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, 
		WINDOW_AGING_TEST_ITEM_WIDGET_Y*2,
		WINDOW_FONT_NORMAL_SIZE * 5,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		"failed:"),	
	[WIDGET_ID_TEST_FAILED_CNT] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_LEFT,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_NORMAL_SIZE*5,
		WINDOW_AGING_TEST_ITEM_WIDGET_Y*2,
		WINDOW_WIDGET_NORMAL_HEIGHT * 4, WINDOW_WIDGET_NORMAL_HEIGHT,
		NULL),
	[WIDGET_ID_TEST_GET_FAILED] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, 
		WINDOW_AGING_TEST_ITEM_WIDGET_Y*3,
		WINDOW_FONT_NORMAL_SIZE * 5,
		WINDOW_WIDGET_NORMAL_HEIGHT,
		"get-err:"),	
	[WIDGET_ID_TEST_GET_FAILED_CNT] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_LEFT,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		WINDOW_FONT_NORMAL_SIZE*5,
		WINDOW_AGING_TEST_ITEM_WIDGET_Y*3,
		WINDOW_WIDGET_NORMAL_HEIGHT * 4, WINDOW_WIDGET_NORMAL_HEIGHT,
		NULL),

};
#if 0
static gkt_date_time_s s_rtc_test_set_dt = {
	.date = {
		.year = 2023,
		.month = 11,
		.day = 27,
	},
	.time = {
		.hour = 10,
		.minute = 0,
		.second = 0,
	},
};
#endif
typedef struct{
	uint32_t success_cnt;
	uint32_t failed_cnt;
	uint32_t get_failed_cnt;
	uint32_t alarm_systime_ms;
	uint32_t alarm_cnt;
}window_test;
static window_test s_window_test;

void window_test_set_rtc_alarm_cb(void)
{
	printf("window_test_set_rtc_alarm_cb: alarm_cnt=%u, time_ms=%u\n", 
		s_window_test.alarm_cnt, gkt_systime_ms()-s_window_test.alarm_systime_ms);
}

#define WINDOW_TEST_DELAY_TIME_S 5
static void test_task(void *params)
{
	//char buff[32];
	int retval;
	int i = 0;
	//gkt_date_time_s date_time;
	uint32_t alarm_time_us = 3*1000*1000;

	gkt_rtc_register_alarm_callback(window_test_set_rtc_alarm_cb);
	while (1) {
#if 0
		retval = gkt_rtc_set_time(&s_rtc_test_set_dt);
		if (retval == GKT_SUCCESS) {
			s_window_test.success_cnt++;
			gkt_printf("gkt_rtc_set_time: set_dt = %04u-%02u-%02u %02u:%02u:%02u\n",
					s_rtc_test_set_dt.date.year, s_rtc_test_set_dt.date.month, 
					s_rtc_test_set_dt.date.day, s_rtc_test_set_dt.time.hour, 
					s_rtc_test_set_dt.time.minute, s_rtc_test_set_dt.time.second);
		}
		else
			s_window_test.failed_cnt++;
#endif
		gkt_thread_sleep(5000);

		retval = gkt_rtc_set_alarm(alarm_time_us, 0);
		s_window_test.alarm_cnt++;
		s_window_test.alarm_systime_ms = gkt_systime_ms();
		if (retval != GKT_SUCCESS)
			gkt_printf("window_test: set_rtc_alarm failed, retval(%d)\n", retval);
		else {
			gkt_printf("window_test: set_rtc_alarm success, time_us(%u), read RTC_WITR - 0x%x\n", 
				alarm_time_us, gkt_read_reg32(0x40040400 + 0x10));
		}
#if 0
		memset(buff, 0, sizeof(buff));
		snprintf(buff, 16, "%u", s_window_test.success_cnt);
		snprintf(&buff[16], 16, "%u", s_window_test.failed_cnt);
		gkt_widget_set_data_by_id(WIDGET_ID_TEST_SUCCESS_CNT, (uint32_t)buff);
		gkt_widget_set_data_by_id(WIDGET_ID_TEST_FAILED_CNT, (uint32_t)&buff[16]);

		for (i=0; i<WINDOW_TEST_DELAY_TIME_S; i++) {
			gkt_thread_sleep(1000);
			retval = gkt_rtc_get_time(&date_time);
			if (retval == GKT_SUCCESS) {
				gkt_printf("gkt_rtc_get_time: get_dt = %04u-%02u-%02u %02u:%02u:%02u\n",
					date_time.date.year, date_time.date.month, date_time.date.day,
					date_time.time.hour, date_time.time.minute, date_time.time.second);
				break;
			}
		}
		if (retval != GKT_SUCCESS) {
			s_window_test.get_failed_cnt++;
			memset(buff, 0, sizeof(buff));
			snprintf(buff, 16, "%u", s_window_test.get_failed_cnt);
			gkt_widget_set_data_by_id(WIDGET_ID_TEST_GET_FAILED_CNT, (uint32_t)buff);
		}
#endif
		gkt_printf("(success_cnt - failed_cnt - get_failed_cnt) (%u - %u - %u)\n",
			s_window_test.success_cnt, s_window_test.failed_cnt, s_window_test.get_failed_cnt);
		gkt_thread_sleep((WINDOW_TEST_DELAY_TIME_S-i)*1000);
		break;

	}
	
}

/* Static Functions ---------------------------------------------*/
static int window_test_event_handler(
					uint32_t major_type, uint32_t minor_type, 
					uint32_t widget_id, uint32_t data)
{
	gkt_thread_t thread;
	switch (major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				gkt_window_system_standby_disable();
				gkt_window_timeout_disable("window_test - INIT");
			}
			else if (GKT_WINDOW_ACTIVE == minor_type) {
				s_window_test.success_cnt = 0;
				s_window_test.failed_cnt = 0;
				s_window_test.get_failed_cnt = 0;
				s_window_test.alarm_cnt = 0;
				s_window_test.alarm_systime_ms = 0;
				window_common_mask_except_cancel_ok();
				//gkt_window_timer_set(TIMER_ID_START, 2000, 0, 0);
				thread = gkt_thread_create(test_task, NULL, 
					GKT_THREAD_PRIORITY_BY_NAME(NORMAL),
					0x1000, NULL);
				if (!thread) {
					gkt_error("window_test_event_handler: create receive_task failed!\n");
				}
			}
			else if (GKT_WINDOW_UNINIT == minor_type) {
				gkt_window_timeout_enable("aging_test_event - UNIT");
				gkt_window_system_standby_enable();
			}
			break;
#if 0
		case GKT_EVENT_TIMER:
			if (TIMER_ID_START == minor_type) {
				retval = gkt_rtc_set_time(&s_rtc_test_set_dt);
				if (retval == GKT_SUCCESS)
					s_window_test.success_cnt++;
				else
					s_window_test.failed_cnt++;
				
				int retval;
				char buff[32];
				memset(buff, 0, sizeof(buff));
				snprintf(buff, 16, "%u", s_window_test.success_cnt);
				snprintf(&buff[16], 16, "%u", s_window_test.failed_cnt);
				gkt_widget_set_data_by_id(WIDGET_ID_TEST_SUCCESS_CNT, (uint32_t)buff);
				gkt_widget_set_data_by_id(WIDGET_ID_TEST_FAILED_CNT, (uint32_t)&buff[16]);

				gkt_window_timer_set(TIMER_ID_START, 3000, 0, 0);
			}
			break;
#endif
		case GKT_EVENT_KEY:
			if ((GKT_KEY_OK == minor_type) || (GKT_KEY_CANCEL == minor_type)) {
				gkt_window_exit();
				return GKT_EVENT_HANDLE_COMPLETE;
			}	
			break;
		default:
			break;
	}

	return GKT_EVENT_HANDLE_CONTINUE;
}

/* Functions ----------------------------------------------------*/
GKT_WINDOW_DESC_DEFINE(test);

